package service 
{
	import cn.inswf.framework.mvcs.Actor;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import view.MediaPlayerView;
	
	/**
	 * ...
	 * @author liwei
	 */
	public class MusicService extends Actor 
	{
		[Inject]
		public var player:MediaPlayerView;
		private var _sound:Sound;
		private var _channel:SoundChannel;
		private var musicSoundtrans:SoundTransform = new SoundTransform();
		private var _init:Boolean;
		private var movieSoundtrans:SoundTransform = new SoundTransform();
		public function MusicService() 
		{
			super();
			_sound = new Sound();
			_sound.addEventListener(IOErrorEvent.IO_ERROR, sound_ioError);
		}
		
		private function sound_ioError(e:IOErrorEvent):void 
		{
			
		}
		
		public function load(mp3:String):void {
			if (mp3 != "") {
				_sound.load(new URLRequest(mp3));
				channel_soundComplete(null);
				_init = true;
			}
		}
		
		private function channel_soundComplete(e:Event=null):void 
		{
			if (_channel != null) {
				_channel.removeEventListener(Event.SOUND_COMPLETE, channel_soundComplete);
			}
			_channel = _sound.play();
			_channel.soundTransform = musicSoundtrans;
			_channel.addEventListener(Event.SOUND_COMPLETE, channel_soundComplete);
		}
		public function setVolume(value:Number):void {
			musicSoundtrans.volume = value;
			if (_init) {
				_channel.soundTransform = musicSoundtrans;
			}
		}
		public function setMovieVolume(value:Number):void {
			player.setVolume(value);
			//movieSoundtrans.volume = e.bytesLoaded / e.bytesTotal;
			//SoundMixer.soundTransform = movieSoundtrans;
		}
	}

}